package homework;

import java.util.Locale;

/**
 * @author DCSGO
 * @version 1.0
 * 格式化姓名
 * 将形式为: Han Shun Ping 的人名
 * 以 Ping,Han.S的形式打印出来
 * 其中.S是中间单词的首字母
 * 例如输入 Willian Jefferson Clinton
 * 输出形式为: Clinton,Willian.J
 */
public class Homework03 {
    public static void main(String[] args) {
        try {
            System.out.println(formatName("Han Shun Ping"));
            System.out.println(formatName("Willian Jefferson Clinton"));
            System.out.println(formatName("Han lao ban"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        //Ping,Han.S
        //Clinton,Willian.J
        //ban,Han.L
    }

    public static String formatName(String name) {
        if (name == null) {
            throw new RuntimeException("传入null");
        }
        String[] nameParts = name.split(" ");
        if (nameParts.length != 3) {
            throw new RuntimeException("姓名必须由三部分组成");
        }
        return String.format("%s,%s.%c",
                nameParts[2],
                nameParts[0],
                nameParts[1].toUpperCase().charAt(0));
    }
}
